package com.itheima.controller;

import com.itheima.pojo.Brand;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@ResponseBody
@Controller
@RequestMapping("/brands")
public class BrandController {
    //查询id  http://localhost:8080/brands/1
    //路径参数  /{param}  @PathVariable参数
    //路径参数  /{param}/{name}  @PathVariable参数, @PathVariable参数
    @GetMapping("/{id}")
    public Brand findById(@PathVariable Integer id) {
        Brand brand = new Brand();
        brand.setId(id);
        brand.setBrandName("黑马");
        brand.setCompanyName("传智");
        return brand;
    }

    //查询全部 http://localhost/brands
    @GetMapping
    public List findAll() {
        ArrayList<Brand> brands = new ArrayList<>();
        Brand brand = new Brand();
        brand.setId(1);
        brand.setBrandName("黑马");
        brand.setCompanyName("传智");
        brands.add(brand);
        brands.add(brand);
        brands.add(brand);
        return brands;
    }

    //增加 http://localhost:8080/brands  post
    @PostMapping
    public String save(Brand brand) {
        System.out.println(brand);
        return "ok";
    }

    //删除http://localhost/brands/1
    @DeleteMapping("/{id}")
    public String delete(@PathVariable Integer id) {
        System.out.println("id=" + id);
        return "delete ok";
    }

    //修改http://localhost/brands
    @PutMapping
    public String update(@RequestBody Brand brand) {
        System.out.println(brand);
        return "update ok";
    }
}
